/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConfigurationGUI.java
 *
 * Created on Apr 12, 2009, 9:37:35 AM
 */
package com.mobiwebinc.compconn.gui;

import com.mobiwebinc.compconn.Main;
import com.mobiwebinc.compconn.client.Authenticator;
import com.mobiwebinc.compconn.configuration.ServerConfiguration;
import com.mobiwebinc.compconn.factory.AnnouncerFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author suraj
 */
public class ConfigurationGUI extends javax.swing.JFrame {

    private static ConfigurationGUI configurationGUI;
    /** Creates new form ConfigurationGUI */
    String oldValue;

    public ConfigurationGUI() {
        initComponents();
        setSize(265, 225);
        if(!AnnouncerFactory.getInstance().isBonjourPresent()) {
            jCheckBox3.setSelected(false);
            jCheckBox3.setEnabled(false);
        }
        try {
            jLabel5.setText(InetAddress.getLocalHost().getHostAddress().toString());
        } catch (UnknownHostException ex) {
            Logger.getLogger(ConfigurationGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jCheckBox3 = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setTitle("Preferences");
        setResizable(false);
        getContentPane().setLayout(null);

        jLabel1.setText("Password");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 40, 80, 30);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jTextField1PropertyChange(evt);
            }
        });
        getContentPane().add(jTextField1);
        jTextField1.setBounds(80, 10, 170, 28);

        jPasswordField1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jPasswordField1PropertyChange(evt);
            }
        });
        getContentPane().add(jPasswordField1);
        jPasswordField1.setBounds(80, 40, 170, 28);

        jLabel2.setText("Port");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(10, 70, 60, 30);

        jTextField4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField4ActionPerformed(evt);
            }
        });
        jTextField4.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jTextField4PropertyChange(evt);
            }
        });
        jTextField4.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField4FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextField4FocusLost(evt);
            }
        });
        getContentPane().add(jTextField4);
        jTextField4.setBounds(80, 70, 60, 28);

        jCheckBox3.setText("Discoverable");
        jCheckBox3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox3ActionPerformed(evt);
            }
        });
        getContentPane().add(jCheckBox3);
        jCheckBox3.setBounds(0, 130, 140, 23);

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(50, 160, 100, 29);

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(150, 160, 100, 29);

        jLabel4.setText("Name");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(10, 16, 80, 20);

        jLabel3.setText("Your IP:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 100, 60, 30);
        getContentPane().add(jLabel5);
        jLabel5.setBounds(80, 96, 160, 40);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ServerConfiguration serverConfiguration = ServerConfiguration.getInstance();
        String password = new String(jPasswordField1.getPassword());
        serverConfiguration.setName(jTextField1.getText());
        if(!password.equals(serverConfiguration.getPassword())) {
            serverConfiguration.setPassword(Authenticator.md5It(password));
        }
        serverConfiguration.setPort(Integer.parseInt(jTextField4.getText()));
        serverConfiguration.setVisible(jCheckBox3.isSelected());
        serverConfiguration.saveServerConfiguration();
        if(JOptionPane.showConfirmDialog(this, "Restart server to apply settings now", "Restart Server", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            Main.getInstance().restart();
        }
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jCheckBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBox3ActionPerformed

    private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField4ActionPerformed

    private void jTextField1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jTextField1PropertyChange
    }//GEN-LAST:event_jTextField1PropertyChange

    private void jPasswordField1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jPasswordField1PropertyChange
    }//GEN-LAST:event_jPasswordField1PropertyChange

    private void jTextField4PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jTextField4PropertyChange
    }//GEN-LAST:event_jTextField4PropertyChange

    private void jTextField4FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField4FocusLost
        try {
            int val = Integer.parseInt(jTextField4.getText());
            if (val < 1 || val > 64000) {
                throw new Exception();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Port should be a number between 1 and 64000", "Invalid port value", JOptionPane.ERROR_MESSAGE);
            jTextField4.setText(oldValue);
        }
    }//GEN-LAST:event_jTextField4FocusLost

    private void jTextField4FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField4FocusGained
        oldValue = jTextField4.getText();
    }//GEN-LAST:event_jTextField4FocusGained

    public static ConfigurationGUI getInstance() {
        if (configurationGUI == null) {
            configurationGUI = new ConfigurationGUI();
        }
        return configurationGUI;
    }

    public void showDialog() {
        ServerConfiguration serverConfiguration = ServerConfiguration.getInstance();
        jTextField1.setText(serverConfiguration.getName());
        jPasswordField1.setText(serverConfiguration.getPassword());
        jTextField4.setText("" + serverConfiguration.getPort());
        jCheckBox3.setSelected(serverConfiguration.isVisible());
        setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration//GEN-END:variables
}
